package cn.software.controller;

import cn.software.entity.User;
import cn.software.entity.param.UserClassParam;
import cn.software.entity.vo.studentVo;
import cn.software.entity.vo.teacherVo;
import cn.software.ex.InsertException;
import cn.software.ex.PasswordNotMatchException;
import cn.software.ex.UserNotFoundException;
import cn.software.ex.UsernameDuplicateException;
import cn.software.mapper.UserClassMapper;
import cn.software.service.UserService;
import cn.software.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result Register(@RequestBody User user) {
        try{
            userService.register(user);
            return Result.success(user);
        }catch (InsertException e) {
            return Result.fail("用户注册出错");
        }catch (UsernameDuplicateException e) {
            return Result.fail("用户名已被占用");
        }
    }

    @RequestMapping("/login")
    public Result login(String username, String password,HttpSession session) {

        try{
            User login = userService.login(username, password);
            //把id和username存储到httpSession
            session.setAttribute("id",login.getId());
            session.setAttribute("username",login.getUsername());
            return Result.success(login);
        } catch (UserNotFoundException e) {
             return Result.fail("该用户不存在");
        } catch (PasswordNotMatchException e){
            return Result.fail("密码错误");
        }
    }

    @GetMapping("/selectTeacher")
    public Result selectTeacher() {
        List<teacherVo> teacher = userService.SelectTeacher();
        if(teacher!=null){
            return Result.success(teacher);
        }else {
            return Result.fail("查询结果为空");
        }

    }

    @GetMapping("/selectStudent")
    public Result selectStudent() {
        List<studentVo> student = userService.selectStudent();
        if (student!=null){
            return Result.success(student);
        }else {
            return Result.fail("查询结果为空");
        }
    }

    //学生根据班级id加入班级
    @PostMapping("/enterClass")
    public Result enterClass(@RequestBody UserClassParam userClassParam){
        boolean isSuccess = userService.enterClass(userClassParam);
        if(isSuccess){
            return Result.success(null);
        }else {
            return Result.fail("加入班级失败");
        }
    }

    @GetMapping("/selectUserById")
    public Result selectUserById(int id) {
        User user = userService.getById(id);
        if(user == null) {
            return Result.fail("无用户信息");
        }
        return Result.success(user);
    }

    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User user) {
        try{
            boolean result = userService.updateUser(user);
            return Result.success(result);
        }catch(UserNotFoundException e) {
            return Result.fail("更新用户信息失败");
        }
    }

}
